/**
 * Renderpack
 * http://renderpack.dev.java.net/
 *
 * COPYRIGHT 2004 O'DELL ENGINEERING LTD.
 */
package net.jwtools.tmt.ui.renderer;

// table stuff
import java.awt.Component;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JTable;


/**
 * A renderer for dates. It uses the {@link SimpleDateFormat} to format Dates as Strings.
 *
 * @author <a href="mailto:jesse@odell.ca">Jesse Wilson</a>
 */
public class DateTableCellRenderer extends javax.swing.table.DefaultTableCellRenderer {
    
    /** formatter to prettify dates */
    private SimpleDateFormat dateFormat = null;

    /**
     * Create a new DateTableCellRenderer that renders Dates as formatted Strings.
     *
     * @param simpleDateFormat a String specifying how to format the date, in the format
     *      specified by {@link SimpleDateFormat}.
     */
    public DateTableCellRenderer(String simpleDateFormat) {
        dateFormat = new SimpleDateFormat(simpleDateFormat);
    }

    /**
     * Returns the component used for drawing the cell. 
     */
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        super.getTableCellRendererComponent(table, null, isSelected, hasFocus, row, column);
        if(value != null) {
            String prettyDate = dateFormat.format((Date)value);
            setText(prettyDate);
        } else {
            setText("");
        }
        return this;
    }
}
